package plantshop.tests;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import plantshop.controller.moves.CreatureTurnMove;
import plantshop.controller.moves.CreatureWalkMove;
import plantshop.model.Creature;
import plantshop.model.Location;

public class CreatureTest
{

	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
	}

	@Before
	public void setUp() throws Exception
	{
	}

	@After
	public void tearDown() throws Exception
	{
	}

	@Test
	public void testCreature()
	{
		Creature c;
		boolean xfailed = false;
		boolean yfailed = false;
		boolean dirXfailed = false;
		boolean dirYfailed = false;
		
		for (int i = 0; i < 1000; i++)
		{
			c = new Creature(1);
			
			xfailed = c.getLocation().getX() > 199 || c.getLocation().getX() < 0 || xfailed;
			yfailed = c.getLocation().getY() > 599 || c.getLocation().getY() < 200 || yfailed;
			dirXfailed = c.getDirX() > 4 || c.getDirX() < -5 || dirXfailed;
			dirYfailed = c.getDirY() > 4 || c.getDirY() < -5 || dirYfailed;
		}
		
		assertFalse(xfailed);
		assertFalse(yfailed);
		assertFalse(dirXfailed);
		assertFalse(dirYfailed);
	}

	@Test
	public void testMakeCreature()
	{
		Creature c;
		boolean xfailed = false;
		boolean yfailed = false;
		boolean dirXfailed = false;
		boolean dirYfailed = false;
		
		for (int i = 0; i < 1000; i++)
		{
			c = Creature.makeCreature(1);
			
			xfailed = c.getLocation().getX() > 199 || c.getLocation().getX() < 0 || xfailed;
			yfailed = c.getLocation().getY() > 599 || c.getLocation().getY() < 200 || yfailed;
			dirXfailed = c.getDirX() > 4 || c.getDirX() < -5 || dirXfailed;
			dirYfailed = c.getDirY() > 4 || c.getDirY() < -5 || dirYfailed;
		}
		
		assertFalse(xfailed);
		assertFalse(yfailed);
		assertFalse(dirXfailed);
		assertFalse(dirYfailed);
	}
	
	@Test
	public void testGetNextMove()
	{
		Creature c = new Creature(1);
		c.setLocation(new Location(100, 100));
		c.setDir(1, 1);
		CreatureWalkMove m = (CreatureWalkMove)c.getNextMove();
		assertEquals(1, m.getX());
		assertEquals(1, m.getY());
		
		c.setLocation(new Location(300, 100));
		c.setDir(1, 1);
		m = (CreatureWalkMove)c.getNextMove();
		assertEquals(-1, m.getX());
		assertEquals(1, m.getY());
		
		c.setLocation(new Location(100, 700));
		c.setDir(1, 1);
		m = (CreatureWalkMove)c.getNextMove();
		assertEquals(1, m.getX());
		assertEquals(-1, m.getY());
		
		c.setLocation(new Location(300, 700));
		c.setDir(1, 1);
		m = (CreatureWalkMove)c.getNextMove();
		assertEquals(-1, m.getX());
		assertEquals(-1, m.getY());
	}

	@Test
	public void testGetNextTure()
	{
		Creature c = new Creature(1);
		CreatureTurnMove m;
		boolean xfailed = false;
		boolean yfailed = false;
		
		for(int i = 0; i < 1000; i++)
		{
			m = (CreatureTurnMove)c.getNextTure();
			xfailed = m.getX() > 4 || m.getX() < -5 || xfailed;
			yfailed = m.getY() > 4 || m.getY() < -5 || yfailed;
		}
		
		assertFalse(xfailed);
		assertFalse(yfailed);
	}
	
	@Test
	public void testGetSetLocation()
	{
		Creature c = new Creature(1);
		c.setLocation(new Location(1234, 5678));
		assertEquals(1234, c.getLocation().getX());
		assertEquals(5678, c.getLocation().getY());
	}
	
	@Test
	public void testGetSetType()
	{
		Creature c = new Creature(1);
		assertEquals(1, c.getType());
		c.setType(2);
		assertEquals(2, c.getType());
	}
	
	@Test
	public void testGetSetDir()
	{
		Creature c = new Creature(1);
		c.setDir(400,  500);
		assertEquals(400, c.getDirX());
		assertEquals(500, c.getDirY());
	}

}
